//
//  OrigPubPhotoTextViewCell.m
//  ShrimpCrowdordering
//
//  Created by jimmy on 2018/8/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "OrigPubPhotoTextViewCell.h"
#import "UIView+Utils.h"

@interface OrigPubPhotoTextViewCell ()<UITextViewDelegate>
{
    CGFloat _cellHeight;
    NSIndexPath * _indexPath;
    NSString * _placeholder;
    NSString * _note;
    BOOL _centerTitleIsHidden;
    BOOL _isSetBtnImage;
}

@property (nonatomic, strong) UITextView  * textView;
@property (nonatomic,strong)  UIButton    * addPicBtn;
@property (nonatomic,strong)  UILabel     * centerLab;
@property (nonatomic, strong) UIImageView * imgView;
@property (nonatomic, strong) UILabel     * noteLab;


@end

@implementation OrigPubPhotoTextViewCell

+ (id)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height placeholder:(NSString *)placeholder note:(NSString *)note centerTitleIsHidden:(BOOL)isHidden btnImage:(BOOL)btnImage
{
    static NSString *cellID = @"OrginalThirdTableViewCell";
    OrigPubPhotoTextViewCell *cell = [[OrigPubPhotoTextViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID andIndexPath:indexPath andCellHeight:height placeholder:placeholder note:note centerTitleIsHidden:isHidden btnImage:btnImage];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height placeholder:(NSString *)placeholder note:(NSString *)note centerTitleIsHidden:(BOOL)isHidden btnImage:(BOOL)btnImage
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _cellHeight = height;
        _indexPath = indexPath;
        _placeholder = placeholder;
        _note = note;
        _centerTitleIsHidden = isHidden;
        _isSetBtnImage = btnImage;
        [self setUIView];
    }
    
    return self;
}

- (void)setUIView
{
    self.textView = [[UITextView alloc] init];
    self.textView.delegate = self;
    self.textView.font = [UIFont systemFontOfSize:15];
    self.textView.layer.cornerRadius = 6;
    self.textView.layer.masksToBounds = YES;
    self.textView.layer.borderColor = [[UIColor grayColor] CGColor];
    self.textView.layer.borderWidth = 1;
    [self addSubview:self.textView];
    
    self.imgView = [[UIImageView alloc] init];
    self.imgView.layer.cornerRadius = 6;
    self.imgView.layer.masksToBounds = YES;
    self.imageView.userInteractionEnabled = YES;
    self.imgView.contentMode = UIViewContentModeScaleAspectFill;
    self.addPicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self addSubview:self.imgView];
    
    UILabel * noteLab = [[UILabel alloc] init];
    noteLab.font = [UIFont systemFontOfSize:12];
    noteLab.textColor = [UIColor colorWithHexString:@"#f21934"];
    [self addSubview:noteLab];
    
    self.centerLab = [[UILabel alloc] init];
    self.centerLab.font = [UIFont systemFontOfSize:15];
    self.centerLab.textColor = [UIColor colorWithHexString:@"#888888"];
    self.centerLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.centerLab];
    
    
    self.addPicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addPicBtn.contentMode = UIViewContentModeScaleAspectFill;
    [self.addPicBtn addTarget:self action:@selector(addPic) forControlEvents:UIControlEventTouchUpInside];
    self.imgView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self addSubview:self.addPicBtn];
    
    
    self.textView.frame = CGRectMake(ScreenX375(31), 2, SCREEN_WIDTH - ScreenX375(31)*2, _cellHeight - ScreenX375(4));
    if (_placeholder != nil) {
        self.textView.placeHoldString = _placeholder;
        self.textView.hidden = false;
        self.imgView.hidden = true;
        noteLab.hidden = true;
        self.centerLab.hidden = true;
    }else{
        self.textView.hidden = true;
        self.imgView.hidden = false;
        noteLab.hidden = false;
        self.centerLab.hidden = false;
    }
    
    CGFloat imgViewHeight =  _cellHeight - ScreenX375(4);
    noteLab.hidden = true;
    self.centerLab.hidden = _centerTitleIsHidden;
    if (!_centerTitleIsHidden) {
        imgViewHeight = _cellHeight - ScreenX375(24);
        
        noteLab.hidden = false;
        noteLab.text = _note;
        noteLab.frame = CGRectMake(ScreenX375(31), _cellHeight - ScreenX375(14), SCREEN_WIDTH - ScreenX375(31)*2, ScreenX375(14));
        self.centerLab.text = @"上传图片";
        self.centerLab.frame = CGRectMake(ScreenX375(31), (_cellHeight - ScreenX375(24))/2, SCREEN_WIDTH - 31*2, ScreenX375(20));
    }else{
        self.imgView.backgroundColor = [UIColor whiteColor];
    }
    
    if (_isSetBtnImage) {
        [self.addPicBtn setImage:[UIImage imageNamed:@"+"] forState:UIControlStateNormal];
    }
    
    self.imgView.frame = CGRectMake(ScreenX375(31), 2, SCREEN_WIDTH - ScreenX375(31)*2, imgViewHeight);
    self.addPicBtn.frame = CGRectMake(ScreenX375(31), 2, SCREEN_WIDTH - ScreenX375(31)*2, imgViewHeight);
    
    self.noteLab = noteLab;
}

- (void)hiddenNote:(BOOL)isHidden
{
    self.noteLab.hidden = isHidden;
}
- (void)hiddenCenterTitle:(BOOL)isHidden
{
    self.centerLab.hidden = isHidden;
}

- (void)hiddenButton:(BOOL)isHidden
{
    self.addPicBtn.hidden = YES;
}

- (void)hiddenButtonBackImage
{
    [self.addPicBtn setImage:nil forState:UIControlStateNormal];
}

- (void)setSelectImage:(UIImage *)img
{
    if (img != nil) {
        self.imgView.image = img;
    }
}

-(void)addPic
{
    if (_addPicsBlock) {
        _addPicsBlock();
    }
}

- (void)setInputText:(NSString *)text
{
    self.textView.text = text;
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (self.inputChangeBlock) {
        self.inputChangeBlock(self.textView.text);
    }
}


@end
